▣ 영상처리 프로그램 만들기¶

In [1]:
## ▣ 필터 프로그램 만들기### Packages
import cv2
import numpy as np
import easygui

In [2]:
### 영상파일 선택
img_file = easygui.fileopenbox()

### 영상 읽기
img_raw = cv2.imread(img_file)

### 영상 출력 - Display an image in an OpenCV window
win_name = "Display window"
img_out = img_raw.copy()
cv2.imshow(win_name, img_out)

### 영상 처리
while True:
    ### 키보드 입력
    k = cv2.waitKey(0)

    ### 원본 영상
    if k == ord("o"):
        img_out = img_raw.copy()
    
    ### color 영상을 Grayscale 영상으로 변환
    if k == ord("g"):
        img_out = cv2.cvtColor(img_raw, cv2.COLOR_BGR2GRAY)

    ### 반전
    if k == ord("r"):
        img_out = cv2.convertScaleAbs(img_raw, alpha = -1, beta = 255)

    ### 샤프닝 필터
    if k == ord("1"):
        mask = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
        img_out = cv2.filter2D(img_raw, -1, mask)

    ### 영상 출력
    cv2.imshow(win_name, img_out)
        
    ### 영상 저장
    if k == ord("s"):
        img_out_name = easygui.filesavebox("파일 이름 입력")
        cv2.imwrite(img_out_name, img_out)
        
    ### 종료
    if k == ord("q"):
        cv2.destroyAllWindows()
        break